# -*- coding: utf-8 -*-
import requests,time

HTTP_Settings={
    "Headers":{
        'user-agent':"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"

    },
    "Timeout":60,
    "reTry":5
}
def set(key,value):
    global HTTP_Settings

def request(method, url, **kwargs):
    global HTTP_Settings
    kwargs.setdefault('headers', HTTP_Settings.get("Headers"))
    kwargs.setdefault('timeout', HTTP_Settings.get("Timeout"))
    kwargs.setdefault('try', HTTP_Settings.get("reTry"))
    trytimes=kwargs.pop("try")
    res_code=0;
    count=0;
    while True:
        count+=1
        res=requests.request(method=method, url=url, **kwargs)
        if res.status_code == 200:
            break
        else:
            if count >= trytimes:
                break
            time.sleep(0.02*count*count*count)

    return res


def client():
    client=requests.Session()
    client.headers.update(HTTP_headers)
    return client

def get(url, params=None, **kwargs):
    kwargs.setdefault('allow_redirects', True)
    return request('get', url, params=params, **kwargs)

def post(url, data=None, json=None, **kwargs):
    return request('post', url, data=data, json=json, **kwargs)

def head(url, **kwargs):
    kwargs.setdefault('allow_redirects', False)
    return request('head', url, **kwargs)

def put(url, data=None, **kwargs):
    return request('put', url, data=data, **kwargs)

def delete(url, **kwargs):
    return request('delete', url, **kwargs)

def options(url, **kwargs):
    kwargs.setdefault('allow_redirects', True)
    return request('options', url, **kwargs)
